package com.code;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.LinearLayout;

import com.Global.Global;
import com.example.R;

public class Chart_DoanhThu extends Activity {

	private int iMonthNow = Calendar.getInstance().get(Calendar.MONTH) + 1;
	private int iYearNow = Calendar.getInstance().get(Calendar.YEAR);
	private List<Integer> tien = new ArrayList<Integer>();
	private GraphicalView mChart;
	private LinearLayout chartView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chart_doanhthu);
		chartView = (LinearLayout) findViewById(R.id.chart);
		execute();
	}

	private List<Timestamp> getDoanhThu() {
		List<Timestamp> dt = new ArrayList<Timestamp>();
		StringBuffer sql = new StringBuffer(
				" SELECT Ngay,MONTH(Ngay) Thang, SUM(CASE WHEN ISNULL(Thanh_Tien,'') = '' THEN 0 ELSE Thanh_Tien END )/1000 Tien ");
		sql.append(" FROM PhieuXuat JOIN VatTuXuat ON PhieuXuat.SoCt = VatTuXuat.SoCt ");
		if (iMonthNow != 12)
			sql.append(" WHERE Ngay > = '" + iYearNow + "/" + (iMonthNow - 1)
					+ "/" + 1 + "' AND  Ngay < '" + iYearNow + "/"
					+ (iMonthNow + 1) + "/" + 1 + "'");
		else
			sql.append(" WHERE Ngay > = '" + (iYearNow - 1) + "/" + 1 + "/" + 1
					+ "' AND  Ngay > = '" + iYearNow + "/" + (iMonthNow + 1)
					+ "/" + 1 + "'");
		sql.append(" GROUP BY Ngay, MONTH(Ngay)  ORDER BY MONTH(Ngay),Ngay ");
		ResultSet rs = Global.excuteQuerySQLServer(sql.toString());
		try {
			while (rs.next()) {
				Timestamp iDateIndex = (rs.getTimestamp("Ngay"));
				Integer iTienIndex = rs.getInt("Tien");
				dt.add(iDateIndex);
				tien.add(iTienIndex);
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return dt;
	}

	public void execute() {
		List<Timestamp> date = getDoanhThu();
		// String[] titles = new String[] { "Doanh thu" };

		int count = date.size();
		Date[] dt = new Date[count];
		for (int i = 0; i < count; i++) {
			GregorianCalendar gc = new GregorianCalendar(
					date.get(i).getYear() + 1900, date.get(i).getMonth(), date
							.get(i).getDate());
			dt[i] = gc.getTime();
		}

		Integer[] tienBan = new Integer[tien.size()];
		if (tien != null && tien.size() != 0) {
			for (int i = 0; i < tien.size(); i++) {
				tienBan[i] = tien.get(i);
			}
		}

		// Creating TimeSeries for Visits
		TimeSeries visitsSeries = new TimeSeries("Doanh Thu theo ngày");

		// Adding data to Visits and Views Series
		for (int i = 0; i < dt.length; i++) {
			visitsSeries.add(dt[i], tienBan[i]);
		}
		// Creating a dataset to hold each series
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

		// Adding Visits Series to the dataset
		dataset.addSeries(visitsSeries);

		XYSeriesRenderer visitsRenderer = new XYSeriesRenderer();
		visitsRenderer.setColor(Color.WHITE);
		visitsRenderer.setPointStyle(PointStyle.CIRCLE);
		visitsRenderer.setFillPoints(true);
		visitsRenderer.setLineWidth(2);
		visitsRenderer.setDisplayChartValues(true);

		// Creating a XYMultipleSeriesRenderer to customize the whole chart
		XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();

		multiRenderer.setChartTitle(" Doanh thu theo ngày  (VNĐ/Ngày)");
		multiRenderer.setXTitle("Ngày");
		multiRenderer.setYTitle("Tiền");
		multiRenderer.setZoomButtonsVisible(true);
		multiRenderer.addSeriesRenderer(visitsRenderer);
		// return ChartFactory.getLineChartIntent(context,
		// dataset,multiRenderer);
		mChart = (GraphicalView) ChartFactory.getTimeChartView(
				getBaseContext(), dataset, multiRenderer, "dd-MM-yyyy");
		chartView.addView(mChart);
	}
}
